package xiangya.bysj.client.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import xiangya.bysj.entity.User;
import xiangya.bysj.service.impl.SMSServiceImpl;
import xiangya.bysj.service.impl.UserServiceImpl;
import xiangya.bysj.util.JsonUtil;
import xiangya.bysj.util.StringUtil;

@Controller
@RequestMapping("/client/user")
public class UserClientController {

	@Resource(name = "userService")
	private UserServiceImpl userService;

	@Resource(name = "SMSService")
	private SMSServiceImpl SMSService;

	@RequestMapping("/login")
	@ResponseBody
	public User login(String username, String password) {
		User user = null;
		try {
			user = userService.login(username, password);
		} catch (Exception e) {
			user = null;
			e.printStackTrace();
		}
		return user;
	}

	@RequestMapping("/register")
	@ResponseBody
	public String register(String reqJson, String vercode) {
		String result = null;
		User user = null;
		try {
			user = (User) JsonUtil.getObject(reqJson, User.class);
			if (SMSService.checkVerCode(user.getUserPhone(), vercode).equals(StringUtil.RES_SUCCESS)) {
				userService.save(user);
				result = StringUtil.RES_SUCCESS;
			} else {
				result = StringUtil.RES_CKVERCODE_FAIL;
			}
		} catch (Exception e) {
			result = StringUtil.RES_FAIL;
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/queryByName")
	@ResponseBody
	public List<User> query(String username) {
		List<User> list = null;
		try {
			list = userService.findByName(username);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
